package com.kdtech.analyse.video;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.analyse.AnalyseNews;

/**
 * http://www.gdtv.cn/ 广东卫视、广东电视台
 * @author 小聂
 */
public class GdtvAnalyse implements AnalyseNews{

	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		String[] regex = {
				//http://nc.gdtv.cn/sports/2015/0901/278574.shtml
				"http://v.gdtv.cn/html/.*/[0-9]{4}-[0-9]+-[0-9]+/[0-9]+.html",
				"http://.*.gdtv.cn/[0-9]{4}/[0-9]{4}/[0-9]+.shtml",
				"http://[a-z]*.gdtv.cn/[a-z]*/[0-9]*/[0-9]*/[0-9]*.shtml",
				"http://[a-z]*.gdtv.cn/[a-z]*/[a-z]*/[0-9]*/[0-9]*/[0-9]*.shtml"
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		//对应 url 地址下的  html 文件
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		news.setUrl(url);
		String title = null;
		String desc = null;
		Long date = null;
		Document doc = Jsoup.parse(htmltxt);
		title = doc.select("title").text();
		title = StringUtils.substringBefore(title, "_");
		desc = doc.select("p.describe").text();
		if(StringUtils.isBlank(desc)){
			desc = doc.select("div.article-content").text();
		}
		date = DateUtils.matchDate(doc.select("#title").text());
		if(date==null){
			date = DateUtils.matchDate(doc.select("span.date").text());
		}
		if(date==null){
			date = DateUtils.matchDate(doc.select("span.post-time").text());
		}
		
		news.setDate(date);
		news.setContent(desc);
		news.setTitle(title);
		String author=doc.select("div.source").text();
		if(StringUtils.isBlank(author)){
			author=doc.select("span.source").text();
		}
		news.setAuthor(author);
		return news;
	}

	

	

	
}
